<?php
defined( '_VALID_NVB' ) or die( 'Direct Access to this location is not allowed.' );
function referbox($url,$msg,$s=3)
{ 
	header("Location: index3.php?url=".urlencode($url)."&msg=".urlencode($msg)."&s=$s"); 
}
function message($msg){
	global $tpl;
	$tpl->newBlock("msg");
	$msg1='<div align="center" style="font-weight:bold;width:96%;color:red;  margin:5px; padding:5px; border:solid 1px #D5D5D5">'.$msg."</div>";
	$tpl->assign("msg",$msg1);
}
//Check file
function is_remote($file_name) {
  return strpos($file_name, '://') > 0 ? 1 : 0;
}

function is_remote_file($file_name) {
  return is_remote($file_name) && preg_match("#\.[a-zA-Z0-9]{1,4}$#", $file_name) ? 1 : 0;
}

function is_local_file($file_name) {
  return !is_remote($file_name) && strpos($file_name, '/') !== false && preg_match("#\.[a-zA-Z0-9]{1,4}$#", $file_name) ? 1 : 0;
}

function check_remote_media($remote_media_file) {
  global $config;
  return is_remote($remote_media_file) && preg_match("#\.[".$CONFIG['allowed_mediatypes_match']."]+$#i", $remote_media_file) ? 1 : 0;
}
function check_local_media($local_media_file) {
  global $config;
  return !is_remote($local_media_file) && strpos($local_media_file, '/') !== false && preg_match("#\.[".$CONFIG['allowed_mediatypes_match']."]+$#i", $local_media_file) ? 1 : 0;
}

function check_remote_thumb($remote_thumb_file) {
  return is_remote($remote_thumb_file) && preg_match("#\.[gif|jpg|jpeg|png|bmp]+$#is", $remote_thumb_file) ? 1 : 0;
}

function check_local_thumb($remote_thumb_file) {
  return !is_remote($local_thumb_file) && strpos($local_thumb_file, '/') !== false && preg_match("#\.[gif|jpg|jpeg|png]+$#i", $local_thumb_file) ? 1 : 0;
}

function get_file_extension($file_name) {
  ereg("(.+)\.(.+)", basename($file_name), $regs);
  return strtolower($regs[2]);
}

function get_file_name($file_name) {
  ereg("(.+)\.(.+)", basename($file_name), $regs);
  return $regs[1];
}

function check_media_type($file_name) {
  global $config;
  return (in_array(get_file_extension($file_name), $CONFIG['allowed_mediatypes_array'])) ? 1 : 0;
}

function check_thumb_type($file_name) {
  return (preg_match("#(gif|jpg|jpeg|png)$#is", $file_name)) ? 1 : 0;
}

function check_executable($file_name) {
  if (substr(PHP_OS, 0, 3) == "WIN" && !eregi("\.exe$", $file_name)) {
    $file_name .= ".exe";
  }
  elseif (substr(PHP_OS, 0, 3) != "WIN") {
    $file_name = eregi_replace("\.exe$", "", $file_name);
  }
  return $file_name;
}
function remote_file_exists($url) { // similar to file_exists(), checks existence of remote files
  $url = trim($url);
  if (!preg_match("=://=", $url)) $url = "http://$url";
  if (!($url = @parse_url($url))) {
    return false;
  }
  if (!eregi("http", $url['scheme'])) {
    return false;
  }
  $url['port'] = (!isset($url['port'])) ? 80 : $url['port'];
  $url['path'] = (!isset($url['path'])) ? "/" : $url['path'];
  $fp = fsockopen($url['host'], $url['port'], $errno, $errstr, 30);
  if (!$fp) {
    return false;
  }
  else {
    $head = "";
    $httpRequest = "HEAD ".$url['path']." HTTP/1.1\r\n"
                  ."HOST: ".$url['host']."\r\n"
                  ."Connection: close\r\n\r\n";
    fputs($fp, $httpRequest);
    while (!feof($fp)) {
      $head .= fgets($fp, 1024);
    }
    fclose($fp);

    preg_match("=^(HTTP/\d+\.\d+) (\d{3}) ([^\r\n]*)=", $head, $matches);
    if ($matches[2] == 200) {
      return true;
    }
  }
}
//stren lenght
function trimlen($str,$max){
	$i=0;
	for($i=0;$i<=$max;$i++){
		$ar.=$str[$i]	;
	}
	if(strlen($str)>$max){
		$ar.="...";
	}
	return $ar;
}
//lay chieu dai va chieu rong cua file anh va file flash
function get_width_height($file_name)
{
    if ($image_info = @getimagesize($file_name)) 
	{
      $width_height = " ".$image_info[3];
      $width = $image_info[0];
      $height = $image_info[1];
	 	return $width_height;	
	 }
	 else
	 {
	 	return false;
	 }
}
// $p=trang so,$url duong link,$sql,$maxpage=so trang duoc hien thi, $maxitem= so ban ghi/trang
function phan_trang($p,$url,$sql,$maxpage,$maxitem,$get='p') 
{ 
	global $DB, $tpl;
	$p=isset($p) ? intval($p):1;
	$db=$DB->query($sql);
	$cac_trang=array();
	$total=mysql_num_rows($db);
	$total=ceil($total/$maxitem);
	if ($p>$maxpage) { 
		$num_page=ceil($p/$maxpage); 
		$showpage=($num_page-1)*$maxpage; 
		$end=$showpage+$maxpage; 
		$showpage++; 
	}else  	{ 
		$thispage=1; 
		$showpage=1; 
		$end=$maxpage; 
	} 
	$startpage=$showpage; 
	for ($showpage;$showpage<$end+1;$showpage++) 
	{ 
		if ($showpage<=$total) { 
			if ($p==$showpage) { 
				$list_page.="&nbsp;<span class='clicked'>[".$showpage."]&nbsp;</span>"; 
			}else { 
				$list_page.="<a href='$url&$get=$showpage' class='cactrang' >[".$showpage."]</a> "; 
			} 
		} 
	} 
	if ($num_page>1) { 
		$back=$startpage-1; 
		if ($num_page>2) { 
			$list_page1="<a href='$url&$get=1' class='cactrang' ><img src=css/phantrang/first.gif border='0' width='10px' height='10px'  align='center'/></a> "; 
		} 
		$list_page1.="<a href='$url&$get=$back' class='cactrang' > <img src=css/phantrang/prev.gif border='0' width='14px' height='10px' align='center'/> &nbsp;</a> "; 
	} 
	if ($num_page<ceil($total/$maxpage)&&($total>$maxpage)) { 
		$next=$showpage; 
		$list_page2.=" <a href='$url&$get=$next'  class='cactrang'><img src=css/phantrang/next.gif border='0' width='14px' height='10px'align='center'/> </a>"; 
		$list_page2.=" <a href='$url&$get=$total'  class='cactrang'> <img src=css/phantrang/last.gif border='0' width='10px' height='10px'align='center'/> </a>"; 
	} 
	$list_page=$list_page1.$list_page.$list_page2; 
	if($total > 1)
	{
		if($list_page){
			$cac_trang['cac_trang']="Trang: ".$p."/".$total." &nbsp;&nbsp;".$list_page;
		 }
	}
	$cac_trang['pre']=$list_page1;
	$cac_trang['next']=$list_page2;
	$cac_trang['pages']=$list_page;
	$a=$maxitem*$p-$maxitem;
	$sql=$sql." limit ".$a.",".$maxitem;
	$cac_trang['db']=$DB->query($sql);
	return $cac_trang;
}
// $p=trang so,$url duong link,$sql,$maxpage=so trang duoc hien thi, $maxitem= so ban ghi/trang
function phan_trang_rewrite($p,$url,$sql,$maxpage,$maxitem) 
{ 
	global $DB, $tpl, $lang;
	if($lang=='_eng'){
		$trang="Page(s)";
	}else{
		$trang="Trang";
	}
	$p=isset($p) ? intval($p):1;
	$db=$DB->query($sql);
	$cac_trang=array();
	$total=mysql_num_rows($db);
	$total=ceil($total/$maxitem);
	if ($p>$maxpage) { 
		$num_page=ceil($p/$maxpage); 
		$showpage=($num_page-1)*$maxpage; 
		$end=$showpage+$maxpage; 
		$showpage++; 
	}else  	{ 
		$thispage=1; 
		$showpage=1; 
		$end=$maxpage; 
	} 
	$startpage=$showpage; 
	for ($showpage;$showpage<$end+1;$showpage++) 
	{ 
		if ($showpage<=$total) { 
			if ($p==$showpage) { 
				$list_page.="<span class='clicked'>&nbsp;".$showpage."&nbsp;</span>"; 
			}else { 
				$list_page.="<a href='".$url.$showpage.".htm' class='cactrang' >&nbsp;".$showpage."&nbsp;</a>"; 
			} 
		} 
	} 
	if ($num_page>1) { 
		$back=$startpage-1; 
		if ($num_page>2) { 
			$list_page1="<a href='".$url."1.htm' class='cactrang' ><img src=css/phantrang/first.gif border='0' width='10px' height='10px'  align='center'/></a>"; 
		} 
		$list_page1.="<a href='$url".$back.".htm' class='cactrang' > <img src=css/phantrang/prev.gif border='0' width='14px' height='10px' align='center'/> &nbsp;</a>"; 
	} 
	if ($num_page<ceil($total/$maxpage)&&($total>$maxpage)) { 
		$next=$showpage; 
		$list_page2.=" <a href='".$url.$next.".htm'  class='cactrang'><img src=css/phantrang/next.gif border='0' width='14px' height='10px'align='center'/> </a>"; 
		$list_page2.=" <a href='".$url.$total.".htm'  class='cactrang'> <img src=css/phantrang/last.gif border='0' width='10px' height='10px'align='center'/> </a>"; 
	} 
	$list_page=$list_page1.$list_page.$list_page2; 
	if($total > 1)
	{
		if($list_page){
			$cac_trang['cac_trang']="&nbsp;".$trang.": ".$p."/".$total." &nbsp;&nbsp;".$list_page;
		 }
	}
	$cac_trang['p']=$p;
	$cac_trang['total']=$total;
	$cac_trang['pre']=$list_page1;
	$cac_trang['next']=$list_page2;
	$cac_trang['pages']=$list_page;
	$a=$maxitem*$p-$maxitem;
	$sql=$sql." limit ".$a.",".$maxitem;
	$cac_trang['db']=$DB->query($sql);
	return $cac_trang;
}

function phan_trang_rewrite_exten($p,$url,$sql,$maxpage,$maxitem,$exten='.tpl') 
{ 
	global $DB, $tpl, $lang;
	if($lang=='_eng'){
		$trang="Page(s)";
	}else{
		$trang="Trang";
	}
	$p=isset($p) ? intval($p):1;
	$db=$DB->query($sql);
	$cac_trang=array();
	$total=mysql_num_rows($db);
	$total=ceil($total/$maxitem);
	if ($p>$maxpage) { 
		$num_page=ceil($p/$maxpage); 
		$showpage=($num_page-1)*$maxpage; 
		$end=$showpage+$maxpage; 
		$showpage++; 
	}else  	{ 
		$thispage=1; 
		$showpage=1; 
		$end=$maxpage; 
	} 
	$startpage=$showpage; 
	for ($showpage;$showpage<$end+1;$showpage++) 
	{ 
		if ($showpage<=$total) { 
			if ($p==$showpage) { 
				$list_page.="<span class='clicked'>&nbsp;".$showpage."&nbsp;</span>"; 
			}else { 
				$list_page.="<a href='".$url."_".$showpage.$exten."' class='cactrang' >&nbsp;".$showpage."&nbsp;</a>"; 
			} 
		} 
	} 
	if ($num_page>1) { 
		$back=$startpage-1; 
		if ($num_page>2) { 
			$list_page1="<a href='$url"."_1".$exten."' class='cactrang' ><img src=css/phantrang/first.gif border='0' width='10px' height='10px'  align='center'/></a>"; 
		} 
		$list_page1.="<a href='$url"."_".$back.$exten."' class='cactrang' > <img src=css/phantrang/prev.gif border='0' width='14px' height='10px' align='center'/> &nbsp;</a>"; 
	} 
	if ($num_page<ceil($total/$maxpage)&&($total>$maxpage)) { 
		$next=$showpage; 
		$list_page2.=" <a href='$url"."_".$next.$exten."'  class='cactrang'><img src=css/phantrang/next.gif border='0' width='14px' height='10px'align='center'/> </a>"; 
		$list_page2.=" <a href='$url"."_".$total.$exten."'  class='cactrang'> <img src=css/phantrang/last.gif border='0' width='10px' height='10px'align='center'/></a>"; 
	} 
	$list_page=$list_page1.$list_page.$list_page2; 
	if($total > 1)
	{
		if($list_page){
			$cac_trang['cac_trang']="&nbsp;".$trang.": ".$p."/".$total." &nbsp;&nbsp;".$list_page;
		 }
	}
	$cac_trang['p']=$p;
	$cac_trang['total']=$total;
	$cac_trang['pre']=$list_page1;
	$cac_trang['next']=$list_page2;
	$cac_trang['pages']=$list_page;
	$a=$maxitem*$p-$maxitem;
	$sql=$sql." limit ".$a.",".$maxitem;
	$cac_trang['db']=$DB->query($sql);
	return $cac_trang;
}


function phan_trang_rewrite_news($p,$url,$sql,$maxpage,$maxitem) 
{ 
	global $DB, $tpl, $lang;
	if($lang=='_eng'){
		$trang="Page(s)";
	}else{
		$trang="Trang";
	}
	$p=isset($p) ? intval($p):1;
	$db=$DB->query($sql);
	$cac_trang=array();
	$total=mysql_num_rows($db);
	$total=ceil($total/$maxitem);
	if ($p>$maxpage) { 
		$num_page=ceil($p/$maxpage); 
		$showpage=($num_page-1)*$maxpage; 
		$end=$showpage+$maxpage; 
		$showpage++; 
	}else  	{ 
		$thispage=1; 
		$showpage=1; 
		$end=$maxpage; 
	} 
	$startpage=$showpage; 
	for ($showpage;$showpage<$end+1;$showpage++) 
	{ 
		if ($showpage<=$total) { 
			if ($p==$showpage) { 
				$list_page.="<span class='clicked'>&nbsp;".$showpage."&nbsp;</span>"; 
			}else { 
				$list_page.="<a href='".$url."_".$showpage.".newsp' class='cactrang' >&nbsp;".$showpage."&nbsp;</a>"; 
			} 
		} 
	} 
	if ($num_page>1) { 
		$back=$startpage-1; 
		if ($num_page>2) { 
			$list_page1="<a href='$url"."_1".".newsp' class='cactrang' >Đầu</a>"; 
		} 
		$list_page1.="<a href='$url"."_".$back.".newsp' class='cactrang' > Trước &nbsp;</a>"; 
	} 
	if ($num_page<ceil($total/$maxpage)&&($total>$maxpage)) { 
		$next=$showpage; 
		$list_page2.=" <a href='$url"."_".$next.".newsp'  class='cactrang'>Tiếp theo </a>"; 
		$list_page2.=" <a href='$url"."_".$total.".newsp'  class='cactrang'> Cuối </a>"; 
	} 
	$list_page=$list_page1.$list_page.$list_page2; 
	if($total > 1)
	{
		if($list_page){
			$cac_trang['cac_trang']="&nbsp;".$trang.": ".$p."/".$total." &nbsp;&nbsp;".$list_page;
		 }
	}
	$cac_trang['p']=$p;
	$cac_trang['total']=$total;
	$cac_trang['pre']=$list_page1;
	$cac_trang['next']=$list_page2;
	$cac_trang['pages']=$list_page;
	$a=$maxitem*$p-$maxitem;
	$sql=$sql." limit ".$a.",".$maxitem;
	$cac_trang['db']=$DB->query($sql);
	return $cac_trang;
}
function phan_trang_rewrite_info($p,$url,$sql,$maxpage,$maxitem) 
{ 
	global $DB, $tpl, $lang;
	if($lang=='_eng'){
		$trang="Page(s)";
	}else{
		$trang="Trang";
	}
	$p=isset($p) ? intval($p):1;
	$db=$DB->query($sql);
	$cac_trang=array();
	$total=mysql_num_rows($db);
	$total=ceil($total/$maxitem);
	if ($p>$maxpage) { 
		$num_page=ceil($p/$maxpage); 
		$showpage=($num_page-1)*$maxpage; 
		$end=$showpage+$maxpage; 
		$showpage++; 
	}else  	{ 
		$thispage=1; 
		$showpage=1; 
		$end=$maxpage; 
	} 
	$startpage=$showpage; 
	for ($showpage;$showpage<$end+1;$showpage++) 
	{ 
		if ($showpage<=$total) { 
			if ($p==$showpage) { 
				$list_page.="<span class='clicked'>&nbsp;".$showpage."&nbsp;</span>"; 
			}else { 
				$list_page.="<a href='".$url."_".$showpage.".infp' class='cactrang' >&nbsp;".$showpage."&nbsp;</a>"; 
			} 
		} 
	} 
	if ($num_page>1) { 
		$back=$startpage-1; 
		if ($num_page>2) { 
			$list_page1="<a href='$url_1".".infp' class='cactrang' ><img src=css/phantrang/first.gif border='0' width='10px' height='10px'  align='center'/></a>"; 
		} 
		$list_page1.="<a href='$url_$back".".infp' class='cactrang' > <img src=css/phantrang/prev.gif border='0' width='14px' height='10px' align='center'/> &nbsp;</a>"; 
	} 
	if ($num_page<ceil($total/$maxpage)&&($total>$maxpage)) { 
		$next=$showpage; 
		$list_page2.=" <a href='$url_$next".".infp'  class='cactrang'><img src=css/phantrang/next.gif border='0' width='14px' height='10px'align='center'/> </a>"; 
		$list_page2.=" <a href='$url_$total".".infp'  class='cactrang'> <img src=css/phantrang/last.gif border='0' width='10px' height='10px'align='center'/> </a>"; 
	} 
	$list_page=$list_page1.$list_page.$list_page2; 
	if($total > 1)
	{
		if($list_page){
			$cac_trang['cac_trang']="&nbsp;".$trang.": ".$p."/".$total." &nbsp;&nbsp;".$list_page;
		 }
	}
	$cac_trang['p']=$p;
	$cac_trang['total']=$total;
	$cac_trang['pre']=$list_page1;
	$cac_trang['next']=$list_page2;
	$cac_trang['pages']=$list_page;
	$a=$maxitem*$p-$maxitem;
	$sql=$sql." limit ".$a.",".$maxitem;
	$cac_trang['db']=$DB->query($sql);
	return $cac_trang;
}

// fix chieu dai va chieu rong cua file anh theo mot co nhat dinh
function file_exit($image)
{
	global $CONFIG;
	if ($image)
	{
		$src=$CONFIG['upload_image_path'].$image;
		if ($image && file_exists($src))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}
function fix_Size($w=0,$h=0,$image,$class="",$exten)
{	
		global $CONFIG;
		$width="";
		$height="";
		$src=$CONFIG['upload_image_path'].$image;		
		$image_info = @getimagesize($src);
		if (file_exit($image))
		{			
			if($w > 0 && intval($image_info[0]) > $w)
			{
				$width=" width = '".$w."'";
			}
			if($h > 0 && intval($image_info[1]) > $h)
			{
				$height=" height ='".$h."'";
			}		
			if($class <> "")
			{
				$c=" class = '".$class."'";
			}	
			return "<img ".$width.$height.$c." src='".$src."'".$exten."/>";
		}	
		else
		{
			return "";
		}
}
//ham lay id cua cac san pham nam trng cat
function getID($idc,$table,$colunmnID,$list){
	global $DB;
	global $listID;
	$listID  = $list.",".$idc;	
	$sql="select * from ".$table." where parentid = ".$idc;	
	$db=$DB->query($sql);
	if(mysql_num_rows($db)>0){		
		while($rs=mysql_fetch_array($db)){
			getID($rs[$colunmnID],$table,$colunmnID,$listID);
		}
	}
	return $listID;	
} 
// ham au to link tao ra duong dan tren site
function autolink($idc,$table,$colunmID,$name,$link,$class)
{
	global $DB,$lang;
	//global $listID;
	global $autolink;
	$a="<a href='".$link."&idc=".$idc."' class='".$class."' >";
	//lay ra ten cua cat dang truy cap
	$sqlName="SELECT * FROM $table where ".$colunmID." = ".$idc;
	$db=$DB->query($sqlName);
	$rs=@mysql_fetch_array($db);
	//-------- cong chuoi auto
	if($name=='')
	{
		$space='';
	}
	else
	{
		$space=" \ ";
	}
	$autolink=$a.$rs['name'.$lang]."</a>".$space.$name;
	//kiem tra xem da phai trang con chua
	if(intval($rs['parentid'])>0)
	{
		autolink(intval($rs['parentid']),$table,$colunmID,$autolink,$link,$class);
	}
	return $autolink;
	
} 

// ham loai bo the p
function formatTextFCK($val) {
	$val=trim($val);
	$pos1 = strpos($val,">");
	$pos2 = strrpos($val,"</");
	$len = strlen($val);
	$pos3 = $len-$pos2;	
	$val = trim(substr($val,$pos1+1,-$pos3));
	return $val;
}

// lay thu tu ($table = ten bang , $order=truong thu tu)
function ordering($table,$order='thu_tu'){
	global $DB;
	$sql="SELECT MAX($order) as thu_tu FROM $table";
	$db=$DB->query($sql);
	if($rs=mysql_fetch_array($db)){
		return $rs['thu_tu']+1;
	}else{
		return 0;
	}
}
function auto_order($table,$order='thu_tu',$pid=0){
	global $DB;
	$sql="SELECT MAX($order) as thu_tu FROM $table where parentid=".$pid;
	$db=$DB->query($sql);
	if($rs=mysql_fetch_array($db)){
		return $rs['thu_tu']+1;
	}else{
		return 0;
	}
}
function auto_orders($table,$order='thu_tu',$id_category,$id_name){
	global $DB;
	$sql="SELECT MAX($order) as thu_tu FROM $table where ".$id_name."=".$id_category;
	$db=$DB->query($sql);
	if($rs=mysql_fetch_array($db)){
		return $rs['thu_tu']+1;
	}else{
		return 0;
	}
}
function change_active_order($table,$id,$pid=0){
	global $DB;
	$sql="Select * from ".$table." WHERE parentid=".$pid." order by thu_tu desc";
	$c=$DB->query($sql);
	$info=array();
	$a=array();
	while ($d=mysql_fetch_array($c))
	{
		$thu_tu=compile_post('thu_tu_'.$d[$id]);
		if ($thu_tu!=$d['thu_tu'])
		{
			$a['thu_tu']=$thu_tu;
			$b=$DB->compile_db_update_string($a);
			$sql="UPDATE ".$table." SET ".$b." WHERE ".$id."=".$d[$id];
			$DB->query($sql);
		}
		$active=compile_post('active_'.$d[$id]);
		if ($active!=$d['active'])
		{
			$a['active']=$active;
			$b=$DB->compile_db_update_string($a);
			$sql="UPDATE ".$table." SET ".$b." WHERE ".$id."=".$d[$id];
			$DB->query($sql);
		}
	}
	message("L&#432;u th&#224;nh c&#244;ng !");
}
function change_active_order_paging($table,$id,$db){
	global $DB;
//	$sql="Select * from ".$table." WHERE parentid=".$pid." order by thu_tu desc";
//	$c=$DB->query($sql);
	$c=$db;
	$info=array();
	$a=array();
	while ($d=mysql_fetch_array($c))
	{
		$thu_tu=compile_post('thu_tu_'.$d[$id]);
		if ($thu_tu!=$d['thu_tu'])
		{
			$a['thu_tu']=intval($thu_tu);
			$b=$DB->compile_db_update_string($a);
			$sql="UPDATE ".$table." SET ".$b." WHERE ".$id."=".$d[$id];
			//echo "<br>".$sql;
			$DB->query($sql);
		}
		$active=compile_post('active_'.$d[$id]);
		if ($active!=$d['active'])
		{
			$a['active']=intval($active);
			$b=$DB->compile_db_update_string($a);
			$sql="UPDATE ".$table." SET ".$b." WHERE ".$id."=".$d[$id];
			// "<br>".$sql;
			$DB->query($sql);
		}
	}
	message("L&#432;u th&#224;nh c&#244;ng !");
}
function change_active_order_paging_product_price($table,$id,$db){
	global $DB;
//	$sql="Select * from ".$table." WHERE parentid=".$pid." order by thu_tu desc";
//	$c=$DB->query($sql);
	$c=$db;
	$info=array();
	$a=array();
	while ($d=mysql_fetch_array($c))
	{
		$thu_tu=compile_post('thu_tu_'.$d[$id]);
		if ($thu_tu!=$d['thu_tu'])
		{
			$a['thu_tu']=intval($thu_tu);
			$b=$DB->compile_db_update_string($a);
			$sql="UPDATE ".$table." SET ".$b." WHERE ".$id."=".$d[$id];
			//echo "<br>".$sql;
			$DB->query($sql);
		}
		$active=compile_post('active_'.$d[$id]);
		if ($active!=$d['active'])
		{
			$a['active']=intval($active);
			$b=$DB->compile_db_update_string($a);
			$sql="UPDATE ".$table." SET ".$b." WHERE ".$id."=".$d[$id];
			// "<br>".$sql;
			$DB->query($sql);
		}
		$price=compile_post('price_'.$d[$id]);
		if ($price!=$d['price'])
		{
			$a['price']=doubleval($price);
			$b=$DB->compile_db_update_string($a);
			$sql="UPDATE ".$table." SET ".$b." WHERE ".$id."=".$d[$id];
			// "<br>".$sql;
			$DB->query($sql);
		}
	}
	message("L&#432;u th&#224;nh c&#244;ng !");
}
// upload image
function uploadimage($inputname,$thumnail='yes',$dich=''){
	global $CONFIG,$_FILES,$tpl,$category,$par_page;
	$a=array();
	if($dich==''){
		$dich=$CONFIG['root_path'].$CONFIG['upload_image_path'];
	}else {
		$dich=$dich;
	}
	if($category==''){
		$category=$par_page;
	}
	if ($_FILES[$inputname]['size']){
		$in_image=get_new_file_name($_FILES[$inputname]['name'],clean_url(get_file_name($_FILES[$inputname]['name'])));
		$file_upload=new Upload($dich,'jpg,gif,png,bmp,swf');
		if ($file_upload->upload_file($inputname,2,$in_image))
		{
			//Da upload thanh cong
			//Tao thumbnail
			$a['image']=$file_upload->file_name;
			if($CONFIG['watermark_image']){
				$watermark = WideImage::load($CONFIG['root_path'].$CONFIG['upload_image_path'].$CONFIG['watermark_image']);
				$base = WideImage::load($dich.$a['image']);
				$result = $base->merge($watermark, "right - 10", "bottom - 10", 100);
			    $result->saveToFile($dich.$a['image']);
			}elseif($CONFIG['watermark_text']){
				 $img = WideImage::load($dich.$a['image']);
				 $canvas = $img->getCanvas();
				
				 $canvas->useFont('VARISON.TTF', 13, $img->allocateColor(255, 255, 255));
				 $canvas->writeText("right - 9", "top + 11", $CONFIG['watermark_text']);
				 $canvas->useFont('VARISON.TTF', 13, $img->allocateColor(9, 171, 25));
				 $canvas->writeText("right - 10", "top + 10", $CONFIG['watermark_text']);
				 $img->saveToFile($dich.$a['image']);	
				
			}
		  	deleteimage($dich.$a['image']);
			if($thumnail=='yes'){
				$thumbnail=create_thumb($dich, $file_upload->file_name);
				if ($thumbnail)
				{
					$a['small_image']=$thumbnail['thumb'];
					$a['normal_image']=$thumbnail['normal'];
				}
				else
				{
					$msg.="Kh&#244;ng t&#7841;o &#273;&#432;&#7907;c &#7843;nh thumbnail ! Xem l&#7841;i &#273;&#7883;nh d&#7841;ng file !<br>";
				}
			}
		}
		else
		{
			$msg.=$file_upload->get_upload_errors()."<br>";
		}			
	}else{
		//Kiem tra remote url
		$img_url=compile_post($inputname.'_url');
		$img_url=trim($img_url);
		if ($img_url!="")
		{
			if (remote_file_exists($img_url))
			{
				if (check_remote_thumb($img_url)) 
				{
					//that remote file 's valid
					$a['image']=$img_url;
					if ($thumbnail=create_thumb_url($img_url))
					{
						$a['small_image']=$thumbnail['thumb'];
						$a['normal_image']=$thumbnail['normal'];
					}
					else
					{
						$msg.="Kh&#244;ng t&#7841;o &#273;&#432;&#7907;c &#7843;nh thumbnail ! H&#227;y xem l&#7841;i &#273;&#7883;nh d&#7841;ng file !<br>";
					}
					
				}
				else
				{
					$msg.="&#272;&#7883;nh d&#7841;ng file kh&#244;ng &#273;&#250;ng!<br>";
				}
			}
			else
			{
				$msg.="Kh&#244;ng truy nh&#7853;p &#273;&#432;&#7907;c file theo URL &#273;&#227; nh&#7853;p !<br>";
			}
		}
	}
	$tpl->newBlock("msg");
	$tpl->assign("msg",$msg);
	return $a;
}


// upload image
function uploadimagenothumb($inputname,$dich=''){
	global $CONFIG,$_FILES,$tpl,$category,$par_page;
	$a=array();
	if($dich==''){
		$dich=$CONFIG['root_path'].$CONFIG['upload_image_path'];
	}else {
		$dich=$dich;
	}
	if($category==''){
		$category=$par_page;
	}
	if ($_FILES[$inputname]['size']){
		$in_image=get_new_file_name($_FILES[$inputname]['name'],clean_url(get_file_name($_FILES[$inputname]['name'])));
		$file_upload=new Upload($dich,'jpg,gif,png,bmp,swf');
		if ($file_upload->upload_file($inputname,2,$in_image))
		{
			//Da upload thanh cong
			//Tao thumbnail
			$a['image']=$file_upload->file_name;
			
			
		}
		else
		{
			$msg.=$file_upload->get_upload_errors()."<br>";
		}			
	}
	
	$tpl->newBlock("msg");
	$tpl->assign("msg",$msg);
	return $a;
}



// $lastfile=image - $lastnormal= normal_image , $lastsmall=small_image
function deleteimage($lastfile='',$lastnormal='',$lastsmall='',$dich=''){ 
	global $CONFIG;
	if($dich==''){
		$dich=$CONFIG['root_path'].$CONFIG['upload_image_path'];
	}else {
		$dich=$dich;
	}
	if ($lastfile||$lastnormal||$lastsmall)
	{
		if ($lastfile && file_exists($dich.$lastfile))
		{
			unlink($dich.$lastfile);
		}
		if ($lastnormal && file_exists($dich.$lastnormal))
		{
			unlink($dich.$lastnormal);
		}
		if ($lastsmall && file_exists($dich.$lastsmall))
		{
			unlink($dich.$lastsmall);
		}						
	}
}
// upload file
function uploadfile($inputname,$dich=''){
	global $CONFIG,$_FILES,$tpl,$category,$par_page;
	$a=array();
	if($dich==''){
		$dich=$CONFIG['root_path'].$CONFIG['upload_media_path'];
	}else {
		$dich=$dich;
	}
	if($category==''){
		$category=$par_page;
	}
	if ($_FILES[$inputname]['size']){
		$in_image=get_new_file_name($_FILES[$inputname]['name'],$_FILES[$inputname]['name']);
		$file_upload=new Upload($dich,$CONFIG['allowed_mediatypes']);
		if ($file_upload->upload_file($inputname,2,$in_image))
		{
			$a['file']=$file_upload->file_name;
		}
		else
		{
			$msg.=$file_upload->get_upload_errors()."<br>";
		}			
	}
	$tpl->newBlock("msg");
	$tpl->assign("msg",$msg);
	return $a;
}
function deletefile($lastfile='',$dich=''){ 
	global $CONFIG;
	if($dich==''){
		$dich=$CONFIG['root_path'].$CONFIG['upload_media_path'];
	}else {
		$dich=$dich;
	}
	if ($lastfile)
	{
		if ($lastfile && file_exists($dich.$lastfile))
		{
			unlink($dich.$lastfile);
		}
						
	}
}
//doc thu 
function getwday($number){
	
	switch($number){
		case 0:
			$thu="Ch&#7911; nh&#7853;t";
			break;
		case 1:
			$thu="Th&#7913; hai";
			break;
		case 2:
			$thu="Th&#7913; ba";
			break;
		case 3:
			$thu="Th&#7913; t&#432;";
			break;
		case 4:
			$thu="Th&#7913; n&#259;m";
			break;
		case 5:
			$thu="Th&#7913; s&#225;u";
			break;
		case 6:
			$thu="Th&#7913; b&#7849;y";
			break;
	}
	return $thu;
}
function getwday_eng($number){
	
	switch($number){
		case 0:
			$thu="Sun";
			break;
		case 1:
			$thu="Mon";
			break;
		case 2:
			$thu="Tue";
			break;
		case 3:
			$thu="Web";
			break;
		case 4:
			$thu="Thu";
			break;
		case 5:
			$thu="Fri";
			break;
		case 6:
			$thu="Sat";
			break;
	}
	return $thu;
}
function checkDelCategory($id,$category,$id_category,$item)
{
	global $DB;
	$sql="select * from ".$category." where parentid = ".$id;
	$db=$DB->query($sql);	
	if(mysql_num_rows($db)>0)
	{
		message("B&#7841;n kh&#244;ng th&#7875; x&#243;a $cat_name n&#224;y v&#236; v&#7851;n c&#242;n $cat_name con b&#234;n trong.h&#227;y x&#243;a c&#225;c $cat_name con tr&#432;&#7899;c!");
		return false;
	}
	else
	{
		$sql1="select * from ".$item." where ".$id_category." = ".$id;
		$db1=$DB->query($sql1);	
		if(mysql_num_rows($db1)>0)
		{
			message("B&#7841;n kh&#244;ng th&#7875; x&#243;a $cat_name n&#224;y v&#236; v&#7851;n c&#242;n $item_name  b&#234;n trong.h&#227;y x&#243;a c&#225;c $item_name con tr&#432;&#7899;c!");
			return false;
		}
		else
		{
			return true;
		}
	}
} 
function strstrim($str,$maxlen=10){
	$str=strip_tags($str,"<br><strong>");
	$a=trim($str);
	$len=strlen($a);
	$string='';
	$dem=0;
	for($i=0;$i<$len;$i++){
		if($a[$i]==' '){
			$dem++;
		}
		if($dem<=$maxlen){
			$string.=$a[$i];
		}
	}
	if($dem>$maxlen){
		$string.="&nbsp;...";
	}
	return $string;
	
}

function RemoveSign($str)
{
	
	$coDau=array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă","ằ","ắ"
	,"ặ","ẳ","ẵ","è","é","ẹ","ẻ","ẽ","ê","ề","ế","ệ","ể","ễ","ì","í","ị","ỉ","ĩ",
	"ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
	,"ờ","ớ","ợ","ở","ỡ",
	"ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
	"ỳ","ý","ỵ","ỷ","ỹ",
	"đ",
	"À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
	,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
	"È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
	"Ì","Í","Ị","Ỉ","Ĩ",
	"Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
	,"Ờ","Ớ","Ợ","Ở","Ỡ",
	"Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
	"Ỳ","Ý","Ỵ","Ỷ","Ỹ",
	"Đ","ê","ù","à");
	$khongDau=array("a","a","a","a","a","a","a","a","a","a","a"
	,"a","a","a","a","a","a",
	"e","e","e","e","e","e","e","e","e","e","e",
	"i","i","i","i","i",
	"o","o","o","o","o","o","o","o","o","o","o","o"
	,"o","o","o","o","o",
	"u","u","u","u","u","u","u","u","u","u","u",
	"y","y","y","y","y",
	"d",
	"A","A","A","A","A","A","A","A","A","A","A","A"
	,"A","A","A","A","A",
	"E","E","E","E","E","E","E","E","E","E","E",
	"I","I","I","I","I",
	"O","O","O","O","O","O","O","O","O","O","O","O"
	,"O","O","O","O","O",
	"U","U","U","U","U","U","U","U","U","U","U",
	"Y","Y","Y","Y","Y",
	"D","e","u","a");
	return str_replace($coDau,$khongDau,$str);
}



function compile($str){
	$str=trim($str);
	$str = preg_replace("/[^a-zA-Z0-9\s]/", "", $str);
	$str=str_replace(" ","-",$str);
	$str=str_replace("--","-",$str);
	$str=str_replace("---","-",$str);
	return $str=strtolower($str);
}
function compile_title($str){
	$str=str_replace("-"," ",$str);
	$str=str_replace("/"," - ",$str);
	return $str;
}
function compile_title_menu($str){
	$str = preg_replace("/[^a-zA-Z0-9\s]/", "", $str);
	$str=str_replace(" ","-",$str);
	$str=str_replace("/","-",$str);
	return $str;
}
function clean_url($text){
	return $str=substr(compile(RemoveSign($text)),0,50);
}
function sendmail($to,$subject,$contentmail, $name)	{
	global $CONFIG;
	$host1='localhost';
	$smtpus='';
	$smtppas='';
	$sitemail=$CONFIG['site_email'];
	//$to='suongmumc@gmail.com';
	//date_default_timezone_set('America/Toronto');
	date_default_timezone_set(date_default_timezone_get());
	
	
	//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
	
	$mail = new PHPMailer();
	$body = $contentmail;
	$body = eregi_replace("[\]",'',$body);
	
	
	
	$mail->Mailer ="smtp";
	$mail->SMTPSecure="tls"; // telling the class to use SMTP
	$mail->IsSMTP();
	$mail->Host = $CONFIG['smtp_host']; // SMTP server
	$mail->SMTPAuth = true;
	$mail->Username = $CONFIG['smtp_username'];
	$mail->Password =$CONFIG['smtp_password'];
	$mail->From = 'mail@orientpc.com.vn';
	$mail->FromName = $name;
	$mail->AddReplyTo($sitemail, $name);
	$mail->WordWrap = 50; // set word wrap
	
	$mail->IsHTML(true);
	 $mail->CharSet  = 'UTF-8'; 
	$mail->Subject = $subject;
	
	//$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
	
	$mail->MsgHTML($body);
	
	$mail->AddAddress($to,$name);
	
	$mail->AddAttachment("images/phpmailer.gif"); // attachment
	
	if(!$mail->Send()) {
		echo "Mailer Error: " . $mail->ErrorInfo;
	} else {
		//echo "Message sent!";
	}
}
function yahoo_nickname_status ($nickname) {
  $response =file_get_contents("http://opi.yahoo.com/online?u=".$nickname."&m=t");
	if($response==$nickname.' is NOT ONLINE'){
		return 0;
	}else{
		return 1;	
	}
}
function check_yahoo($nickname,$type)
{    	
	if($type==0)
	{
		$check_on = "<a href='ymsgr:sendIM?$nickname'><img src='http://opi.yahoo.com/online?u=$nickname&amp;m=g&amp;t=2' border='0'></a>";
	}
	else
	{
		$check_on = "<a href='skype:{$nickname}?chat'><img border='0' src='http://mystatus.skype.com/smallicon/{$nickname}' ></a>";
	}
	return $check_on;
}   
function fck($field, $value, $width, $height)
{
        $fck = new FCKeditor($field);
        $fck->Height = $height;
		$fck->Width = $width;
        $fck->Value = $value;
		if($basepath)
		//$fck->BasePath =$basepath;
        return $fck->CreateHtml();
}
function fckbase($field, $value, $width, $height)
{
	$fck = new FCKeditor($field);
	$fck->ToolbarSet = "Basic";
	$fck->Height = $height;
	$fck->Width = $width;
	$fck->Value = $value;
	if($basepath)
	//	$fck->BasePath =$basepath;
	return $fck->CreateHtml();
}
function alert($alert)
{
	$alert = "<script languague='javascript'>alert('$alert');</script>";
	echo $alert;
}
function embed_flash($url,$width,$height){
	$str='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"           
	codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"
    width="'.$width.'" height="'.$width.'" id="Flash" align="">
    <param name="movie" value="'.$url.'">
    <param name="quality" value="high">
    <param name="bgcolor" value="#FFFFFF">     
    <param name="wmode" value="transparent">
    <embed src="'.$url.'" quality="high" bgcolor="#FFFFFF"  width="'.$width.'"
            height="'.$height.'" name="Flash" align="" wmode="transparent"
            type="application/x-shockwave-flash"
            pluginspage="http://www.macromedia.com/go/getflashplayer">         
    </embed>
	</object>';
	return $str;
}
function get_timestamp_form_binh($ngay,$thang,$nam)
{
	return mktime(0, 0, 0, $thang, $ngay, $nam);
}
function strip_date($datestring){
	$a=array();
	$a=explode("/",$datestring);
	return mktime(0, 0, 0, $a[1], $a[0], $a[2]);
}
function strip_date_time($datestring){
	$datestring=str_replace(" ","/",$datestring);
	$datestring=str_replace(":","/",$datestring);

	$a=array();
	$a=explode("/",$datestring);
	return mktime($a[3],$a[4],$a[5],  $a[1], $a[0], $a[2]);
}
function get_link_extension($link) {
  ereg("(.+)\.(.+)", basename($link), $regs);
  return strtolower($regs[2]);
}

function process_link($link,$id){
	if($link!=''){
		$linklen=strlen($link);
		$linkex=get_link_extension($link);
		$linkexlen=strlen($linkex);
		return $link=substr($link,0,$linklen-$linkexlen-1)."_".$id.".".$linkex;
	}
}
function setvitri($id_menu){
	global $DB,$lang;
	$vitri=array();
	$id_menu=intval($id_menu)	;
	$sql="SELECT vitri FROM menu".$lang." WHERE id_menu=$id_menu";
	$db=$DB->query($sql);
	$vt='';
	if($rs=mysql_fetch_array($db)){
		$vt=$rs['vitri'];
	}
	$vitri=explode(",",$vt);
	return $vitri;	
}
function downloadFile($path, $newFileName=null)
{
		

	// change, added quotes to allow spaces in filenames, by Rajkumar Singh
	header("Content-Disposition: attachment; filename=\"".(is_null($newFileName)?basename($path):$newFileName)."\";" );
	header("Content-Transfer-Encoding: binary");
	header("Content-Length: ".filesize($path));

	readfile($path);
	exit();
											
	

}
function phan_trang_gallery($p,$url,$sql,$maxpage,$maxitem,$get='p') 
{ 
	global $DB, $tpl;
	$p=isset($p) ? intval($p):1;
	$db=$DB->query($sql);
	$cac_trang=array();
	$total=mysql_num_rows($db);
	$total=ceil($total/$maxitem);
	if ($p>$maxpage) { 
		$num_page=ceil($p/$maxpage); 
		$showpage=($num_page-1)*$maxpage; 
		$end=$showpage+$maxpage; 
		$showpage++; 
	}else  	{ 
		$thispage=1; 
		$showpage=1; 
		$end=$maxpage; 
	} 
	$startpage=$showpage; 
	for ($showpage;$showpage<$end+1;$showpage++) 
	{ 
		if ($showpage<=$total) { 
			if ($p==$showpage) { 
				$list_page.="&nbsp;<span class='clickedgl'>[".$showpage."]&nbsp;</span>"; 
			}else { 
				$list_page.="<a href='$url&$get=$showpage' class='cactranggl' onclick='return false'>[".$showpage."]</a> "; 
			} 
		} 
	} 
	if ($num_page>1) { 
		$back=$startpage-1; 
		if ($num_page>2) { 
			$list_page1="<a href='$url&$get=1' class='cactranggl'  onclick='return false'><img src=css/phantrang/first.gif border='0' width='10px' height='10px'  align='center'/></a> "; 
		} 
		$list_page1.="<a href='$url&$get=$back' class='cactranggl' onclick='return false' > <img src=css/phantrang/prev.gif border='0' width='14px' height='10px' align='center'/> &nbsp;</a> "; 
	} 
	if ($num_page<ceil($total/$maxpage)&&($total>$maxpage)) { 
		$next=$showpage; 
		$list_page2.=" <a href='$url&$get=$next'  class='cactranggl'  onclick='return false'><img src=css/phantrang/next.gif border='0' width='14px' height='10px'align='center'/> </a>"; 
		$list_page2.=" <a href='$url&$get=$total'  class='cactranggl'  onclick='return false'> <img src=css/phantrang/last.gif border='0' width='10px' height='10px'align='center'/> </a>"; 
	} 
	$list_page=$list_page1.$list_page.$list_page2; 
	if($total > 1)
	{
		if($list_page){
			$cac_trang['cac_trang']="Trang: ".$p."/".$total." &nbsp;&nbsp;".$list_page;
		 }
	}
	$cac_trang['pre']=$list_page1;
	$cac_trang['next']=$list_page2;
	$cac_trang['pages']=$list_page;
	$a=$maxitem*$p-$maxitem;
	$sql=$sql." limit ".$a.",".$maxitem;
	$cac_trang['db']=$DB->query($sql);
	return $cac_trang;
}
?>
